package com.sxy.web;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxy.dto.Exposer;
import com.sxy.dto.SeckillExecution;
import com.sxy.dto.SeckillResult;
import com.sxy.entity.Seckill;
import com.sxy.enums.SeckillStatEnum;
import com.sxy.exception.RepeatKillException;
import com.sxy.exception.SeckillException;
import com.sxy.service.SeckillService;

@Controller
@RequestMapping()//url:模块/资源/{id} --> /seckill/list
public class SeckillController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SeckillService seckillService;
	
	@RequestMapping(value = "list",method = RequestMethod.GET)
	public String list(Model model){
		//获取列表
		List<Seckill> list = seckillService.getSeckillList();
		model.addAttribute("list",list);
		
		//list.jsp + model = ModelAndView
		return "list"; // 等于 WEB-INF/jsp/list.jsp
	}
	
	@RequestMapping(value = "/{seckillId}/detail" , method = RequestMethod.GET)
	public String detail(@PathVariable("seckillId") Long seckillId , Model model){
	  if(seckillId == null){
		  return "redirect:/seckill/list";
		  
	  }
		Seckill seckill = seckillService.getById(seckillId);
		if(seckill == null){
			return "forward:/seckill/list";
		}
		model.addAttribute("seckill",seckill);
		return "detail";
	}
	
	//ajax json 接口
	@RequestMapping(value = "/{seckillId}/exposer",
			method = RequestMethod.POST,
			produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public SeckillResult<Exposer> exposer(@PathVariable Long seckillId){
		SeckillResult<Exposer> result;	
		try {
			Exposer exposer = seckillService.exportSeckillUrl(seckillId);
			result = new SeckillResult<Exposer>(true, exposer);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage());
			result = new SeckillResult<Exposer>(false, e.getMessage());
		}	
		return result;
	}
	
	@RequestMapping(value = "/{seckillId}/{md5}/execution" , 
			method = RequestMethod.POST,
			produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public SeckillResult<SeckillExecution> execute(@PathVariable("seckillId") Long seckillId,
			                                       @PathVariable("md5") String md5,
			                                       @CookieValue(value = "killPhone" ,required = false) Long phone ){
		//当验证很多且复杂时用springmvc的 valid
		if(phone == null){
			return new SeckillResult<SeckillExecution>(false, "未注册");
			
		}
		SeckillResult<SeckillExecution> result;			
		try {
			//通过存储过程获取
			SeckillExecution execution = seckillService.executeSeckillProdure(seckillId, phone, md5);			
			return new SeckillResult<SeckillExecution>(true, execution);
		}catch (RepeatKillException e) {
			// TODO: handle exception
			SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.REPEAT_KILL);
			return new SeckillResult<SeckillExecution>(true, execution);
		}catch (SeckillException e) {
			// TODO: handle exception
			SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.END);
			return new SeckillResult<SeckillExecution>(true, execution);
		}catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage(),e);
			SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.INNER_ERROR);
			return new SeckillResult<SeckillExecution>(true, execution);
		}		
	}
	
	@RequestMapping(value = "/time/now", method = RequestMethod.GET)
	@ResponseBody
	@DateTimeFormat(pattern="yyyy-MM-dd")
	public SeckillResult<Long> time(){
		Date now = new Date();
		return new SeckillResult<Long>(true,now.getTime());
		
	}
}
